package com.example.day10_zuoye.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.day10_zuoye.R;
import com.example.day10_zuoye.utils.SPUtils;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText name;
    private EditText passwod;
    private CheckBox cb;
    private Button bt_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

        String name2 = SPUtils.getInstance(this).getString("name");
        String password2 = SPUtils.getInstance(this).getString("passord");
        if(!name2.equals("")){
            name.setText(name2);
            passwod.setText(password2);
            cb.setChecked(true);
        }


    }

    private void initView() {
        name = (EditText) findViewById(R.id.name);
        passwod = (EditText) findViewById(R.id.passwod);
        cb = (CheckBox) findViewById(R.id.cb);
        bt_login = (Button) findViewById(R.id.bt_login);

        bt_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                login();

                break;
        }
    }

    private void login() {
        String name_ = name.getText().toString();
        String password_ = passwod.getText().toString();
        if(cb.isChecked()){
            SPUtils.getInstance(this).putString("name",name_);
            SPUtils.getInstance(this).putString("passord",password_);
        }else{
            SPUtils.getInstance(this).putString("name","");
            SPUtils.getInstance(this).putString("passord","");
        }
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);

    }


}
